package com.iversonx.proxy.example_1.controller.v2;

import com.iversonx.proxy.example_1.domain.RequestInfo;
import com.iversonx.proxy.example_1.service.MetricsCollector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 *
 **/
@Component
public class UserControllerProxy implements IUserController {
    private final Logger logger = LoggerFactory.getLogger(UserControllerProxy.class);
    
    @Autowired
    private UserControllerV2 target;
    
    @Autowired
    private MetricsCollector metricsCollector;
    
    @Override
    public void login() {
        logger.info("Enter proxy login");
        long start = System.currentTimeMillis();
        target.login();
        long end = System.currentTimeMillis();
        recordRequest("login", start, end);
        logger.info("Exit proxy login");
    }
    
    @Override
    public void register() {
        logger.info("Enter proxy register");
        long start = System.currentTimeMillis();
        target.register();
        long end = System.currentTimeMillis();
        recordRequest("register", start, end);
        logger.info("Exit proxy register");
    }
    
    private void recordRequest(String type, long start, long end) {
        RequestInfo requestInfo = new RequestInfo();
        requestInfo.setType(type);
        requestInfo.setStartTime(start);
        requestInfo.setResponseTime(end);
        metricsCollector.recordRequest(requestInfo);
    }
}
